package com.cloud.DrugManagement.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.DrugManagement.mapper.DrugsMapper;
import com.cloud.DrugManagement.mapper.StockMapper;
import com.cloud.DrugManagement.pojo.Drugs;
import com.cloud.DrugManagement.service.DrugsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhozixiong
 * @date 2023/02/06
 */
@Service
@RequiredArgsConstructor
public class DrugsServiceImpl extends ServiceImpl<DrugsMapper, Drugs> implements DrugsService {

    private final StockMapper stockMapper;

    private final static int SIZE = 10;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(Drugs drugs) {
        Drugs drugs1 = baseMapper.searchAllByDid(drugs.getDid());
        //判断商品是否存在
        if(drugs1 == null) {
            //添加相应的库存信息
            stockMapper.insert(drugs.getDid(), 0);
            //添加商品
            baseMapper.insert(drugs);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateAllByDid(Drugs drugs) {
        return baseMapper.updateAllByDid(drugs) >= 1;
    }

    @Override
    public boolean updateRetailPriceByDid(double retailPrice, int did) {
        return baseMapper.updateRetailPriceByDid(retailPrice, did) >= 1;
    }

    @Override
    public Page<Drugs> userSearchDrugs(int pages, Object parameter) {
        Page page = new Page(pages, SIZE);
        return baseMapper.userSearchDrugs(page, parameter);
    }

    @Override
    public Page<Drugs> adminSearchDrugs(int pages, Object parameter) {
        Page page = new Page(pages, SIZE);
        return baseMapper.adminSearchDrugs(page, parameter);
    }

    @Override
    public boolean delByDid(Integer did) {
        return baseMapper.delByDid(did) >= 1;
    }
}
